package com.zjc615.service.impl;

import com.zjc615.api.param.MallUserUpdateParam;
import com.zjc615.common.Constants;
import com.zjc615.common.NewBeeMallException;
import com.zjc615.common.ServiceResultEnum;
import com.zjc615.dao.MallUserMapper;
import com.zjc615.dao.NewBeeMallUserTokenMapper;
import com.zjc615.entity.MallUser;
import com.zjc615.entity.MallUserToken;
import com.zjc615.service.NewBeeMallUserService;
import com.zjc615.util.MD5Util;
import com.zjc615.util.NumberUtil;
import com.zjc615.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewBeeMallUserServiceImpl implements NewBeeMallUserService {

    @Autowired
    private MallUserMapper mallUserMapper;
    @Autowired
    private NewBeeMallUserTokenMapper newBeeMallUserTokenMapper;

    @Override
    public String register(String loginName, String password) {
        if (mallUserMapper.selectByLoginName(loginName) != null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        MallUser registerUser = new MallUser();
        registerUser.setLoginName(loginName);
        registerUser.setNickName(loginName);
        registerUser.setIntroduceSign(Constants.USER_INTRO);
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        registerUser.setPasswordMd5(passwordMD5);
        if (mallUserMapper.insertSelective(registerUser) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String login(String loginName, String passwordMD5) {
        MallUser user = mallUserMapper.selectByLoginNameAndPassword(loginName, passwordMD5);
        if (user != null)
        {
            // 检测账号是否被锁定
            if (user.getLockedFlag() == 1)
            {
                ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult();
            }
            // 登录后执行修改token操作
            String token = getNewToken(System.currentTimeMillis() + "", user.getUserId());
            MallUserToken mallUserToken = newBeeMallUserTokenMapper.selectByPrimaryKey(user.getUserId());
            // 当前时间
            Date now = new Date();
            // 过期时间，两天后过期
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000);

            if (mallUserToken == null)
            {
                mallUserToken = new MallUserToken();
                mallUserToken.setUserId(user.getUserId());
                mallUserToken.setToken(token);
                mallUserToken.setUpdateTime(now);
                mallUserToken.setExpireTime(expireTime);

                // 新增一条token数据
                if (newBeeMallUserTokenMapper.insertSelective(mallUserToken) > 0)
                {
                    // 新增成功返回token
                    return token;
                }
            }
            else
            {
                mallUserToken.setToken(token);
                mallUserToken.setUpdateTime(now);
                mallUserToken.setExpireTime(expireTime);
                // 更新
                if (newBeeMallUserTokenMapper.updateByPrimaryKeySelective(mallUserToken) > 0)
                {
                    // 新增成功返回token
                    return token;
                }
            }
        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }

    @Override
    public Boolean updateUserInfo(MallUserUpdateParam mallUser, Long userId) {
        // 通过用户id获取到用户
        MallUser user = mallUserMapper.selectByPrimaryKey(userId);
        // 如果获取不到用户就返回 “未查询到记录！”
        if (user == null) {
            NewBeeMallException.fail(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        // 修改昵称
        user.setNickName(mallUser.getNickName());
        //user.setPasswordMd5(mallUser.getPasswordMd5());
        //若密码为空字符，则表明用户不打算修改密码，使用原密码保存
        if (!MD5Util.MD5Encode("", "UTF-8").equals(mallUser.getPasswordMd5())){
            user.setPasswordMd5(mallUser.getPasswordMd5());
        }
        // 修改简介
        user.setIntroduceSign(mallUser.getIntroduceSign());
        // 去给 dao 层修改数据库信息
        if (mallUserMapper.updateByPrimaryKeySelective(user) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean logout(Long userId) {
        return newBeeMallUserTokenMapper.deleteByPrimaryKey(userId) > 0;
    }

    /**
     * 获取token 值
     * @param timeStr
     * @param userId
     * @return
     */
    public String getNewToken(String timeStr, Long userId){
        String src = timeStr + userId + NumberUtil.genRandomNum(4);
        return SystemUtil.genToken(src);
    }
}
